﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using 群智感知激励系统服务端底层;

namespace 群智感知激励系统服务端
{
    public partial class 实时监控窗口 : CCSkinMain
    {
        Stopwatch stopwatch = new Stopwatch();
        public 实时监控窗口()
        {
            InitializeComponent();
            TCP传输层.启动();
        }

        private void 实时监控窗口_Load(object sender, EventArgs e)
        {
            chart1.Series.Clear();
            timer1.Enabled = true;
            stopwatch.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (用户 i in 用户.删除列表)
            {
                #region 更新图表
                Series s = chart1.Series.FindByName(i.临时id.ToString() + "的信息匿名度");
                if (s != null)
                {
                    chart1.Series.Remove(s);
                }
                s = chart1.Series.FindByName(i.临时id.ToString() + "的货币匿名度");
                if (s != null)
                {
                    chart1.Series.Remove(s);
                }
                #endregion
            }
            if (用户.用户列表.Count == 0)
                return;

            foreach (用户 i in 用户.用户列表)
            {
                double 信息匿名度 = 100.00 - 100.00 / 用户.用户列表.Count;
                double 货币匿名度 = 100.00 - 100.00 / 用户.用户列表.Count - 0.01 * Math.Pow(i.在线时间, 2);
                if (货币匿名度 < 0)
                    货币匿名度 = 0;
                #region 更新图表
                if (chart1.Series.FindByName(i.临时id.ToString() + "的信息匿名度") == null)
                {
                    Series s = new Series(i.临时id.ToString() + "的信息匿名度");
                    s.ChartType = SeriesChartType.Line;
                    s.BorderWidth = 2;
                    //s.MarkerSize = 5;
                    //s.MarkerStyle = MarkerStyle.Circle;
                    chart1.Series.Add(s);
                }
                if (chart1.Series.FindByName(i.临时id.ToString() + "的货币匿名度") == null)
                {
                    Series s = new Series(i.临时id.ToString() + "的货币匿名度");
                    s.ChartType = SeriesChartType.Line;
                    s.BorderWidth = 2;
                    //s.MarkerSize = 5;
                    //s.MarkerStyle = MarkerStyle.Circle;
                    chart1.Series.Add(s);
                }
                chart1.Series[i.临时id.ToString() + "的信息匿名度"].Points.AddXY((double)stopwatch.ElapsedMilliseconds / 1000, 信息匿名度);
                chart1.Series[i.临时id.ToString() + "的货币匿名度"].Points.AddXY((double)stopwatch.ElapsedMilliseconds / 1000, 货币匿名度);
#endregion
            }
            #region 更新图表
            if (stopwatch.ElapsedMilliseconds / 1000 > 30)
            {
                chart1.ChartAreas["ChartArea1"].AxisX.Minimum = stopwatch.ElapsedMilliseconds / 1000 - 30;
                chart1.ChartAreas["ChartArea1"].AxisX.Maximum = stopwatch.ElapsedMilliseconds / 1000;
            }
            else
            {
                chart1.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
                chart1.ChartAreas["ChartArea1"].AxisX.Maximum = 30;
            }
#endregion
        }
    }
}
